package com.oracle.sbdb.bo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.oracle.sbdb.dao.DepartmentDao;
import com.oracle.sbdb.dao.EngineerDao;
import com.oracle.sbdb.model.Department;
import com.oracle.sbdb.model.Engineer;
import com.oracle.sbdb.servlet.model.EngineerInformation;

public class EngineerBo {
	public static EngineerBo INSTANCE = new EngineerBo();
	
	private static EngineerDao engineerDao = new EngineerDao();
	private static DepartmentDao departmentDao = new DepartmentDao();
	
	private static Map<Integer, String> departmentMap = new HashMap<Integer, String>();
	private static Map<Integer, Integer> departmentIdMap = new HashMap<Integer, Integer>();
	private static Map<Integer, String> engineerGIdMap = new HashMap<Integer, String>();
	private static Map<String, Integer> engineerIdMap = new HashMap<String, Integer>();
	private static Map<Integer, List<Integer>> departmentEngineerMap = new HashMap<Integer, List<Integer>>();
	
	private EngineerBo(){};
	
	public EngineerInformation checkPassword(String email, String password){
		EngineerInformation res = null;
		Engineer engineer = engineerDao.selectbyEMail(email);
		
		if(password.trim().equals(engineer.getPassword().trim())){
			int engineerId = engineer.getEngineer_id();
			String gid = engineer.getGid();
			int departmentId = engineer.getDepartment_id();
			String eMail = engineer.getE_mail();
			String firstName = engineer.getFirst_name();
			String lastName = engineer.getLast_name();
			
			String department = null;
			if(departmentMap.containsKey(departmentId)){
				department = departmentMap.get(departmentId);
			}else{
				Department dep = departmentDao.selectbyId(departmentId);
				departmentMap.put(departmentId, dep.getDepartment_name());
				department = dep.getDepartment_name();
			}
			
			res = new EngineerInformation();
			res.setEngineerId(engineerId);
			res.setGid(gid);
			res.setDepartment(department);
			res.seteMail(eMail);
			res.setFirstName(firstName);
			res.setLastName(lastName);
		}
		return res;
	}
	
	public int getDepartmentIDbyId (int engineerId) {
		if(departmentIdMap.containsKey(engineerId)){
			return departmentIdMap.get(engineerId);
		}else{
			Engineer engineer = engineerDao.selectbyId(engineerId);
			if(engineer != null){
				departmentIdMap.put(engineerId, engineer.getDepartment_id());
				return engineer.getDepartment_id();
			}
		}
		return -1;
	}
	
	public String getEngineerGIdbyId (int engineerId){
		if(engineerGIdMap.containsKey(engineerId)){
			return engineerGIdMap.get(engineerId);
		}else{
			Engineer engineer = engineerDao.selectbyId(engineerId);
			if(engineer != null){
				engineerGIdMap.put(engineerId, engineer.getGid());
				engineerIdMap.put(engineer.getGid(), engineerId);
				return engineer.getGid();
			}else{
				return null;
			}
		}
	}
	
	public int getEngineerIdbyGId(String gid){
		if(engineerIdMap.containsKey(gid)){
			return engineerIdMap.get(gid);
		}else{
			Engineer engineer = engineerDao.selectbyGId(gid);
			if(engineer != null){
				engineerGIdMap.put(engineer.getEngineer_id(), gid);
				engineerIdMap.put(gid, engineer.getEngineer_id());
				return engineer.getEngineer_id();
			}else{
				return -1;
			}
		}
	}
	public List<Integer> selectEngineerIdbyDepartmentId(int departmentId){
		List<Integer> res = null;
		if(departmentEngineerMap.containsKey(departmentId)){
			return departmentEngineerMap.get(departmentId);
		}else{
			List<Engineer> engineerList = engineerDao.selectEngineerbyDepartmentId(departmentId);
			if(engineerList != null){
				res = new ArrayList<Integer>();
				for(Engineer engineer: engineerList){
					res.add(engineer.getEngineer_id());
				}
			}
		}
		return res;
	}
}
